package com.fmboy.MXH.nav.controller;

import com.fmboy.MXH.nav.domain.ErrorCode;
import com.fmboy.MXH.nav.domain.Result.Result;
import com.fmboy.MXH.nav.domain.dto.CategoryDTO;
import com.fmboy.MXH.nav.domain.pojo.CategoryVO;
import com.fmboy.MXH.nav.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@Slf4j
@Tag(name = "分类接口", description = "Category API")
@RestController
@RequestMapping("/mxh-nav/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @GetMapping
    @Operation(summary = "获取-分类", description = "获取-分类")
    public Result<List<CategoryVO>> getCategories(
            @RequestParam(required = false) String category_name,
            @RequestParam(required = false) String state) {

        CategoryDTO categoryDTO = new CategoryDTO();
        categoryDTO.setCategory_name(category_name);
        categoryDTO.setState(state);

        List<CategoryVO> categories = categoryService.listByUserId(categoryDTO);
        if (categories.isEmpty()) {
            log.info("获取分类失败");
            return Result.error(ErrorCode.UNAUTHORIZED, "获取失败");
        }

        log.info("获取分类成功");
        return Result.success("获取分类成功", categories);
    }

    //获取-分类详情
    @GetMapping("/info")
    @Operation(summary = "获取-分类详情", description = "获取-分类详情")
    public Result<CategoryVO> getCategoryDetail(@RequestParam Integer id) {
        try {
            CategoryVO categoryVO = categoryService.ByUserId(id);
            return Result.success("获取分类详情成功", categoryVO);
        } catch (Exception e) {
            return Result.error(ErrorCode.UNAUTHORIZED, e.getMessage());
        }
    }

    //新增分类
    @PostMapping()
    @Operation(summary = "新增分类", description = "新增分类")
    public Result<String> addCategory(@RequestBody CategoryDTO categoryDTO) {
        try {
            categoryService.saveCategory(categoryDTO);
            return Result.success("新增分类成功");
        } catch (Exception e) {
            return Result.error(ErrorCode.UNAUTHORIZED, e.getMessage());
        }
    }

    //修改分类
    @PutMapping
    @Operation(summary = "修改分类", description = "修改分类")
    public Result<String> updateCategory(@RequestBody CategoryDTO categoryDTO) {
        try {
            categoryService.updateCategory(categoryDTO);
            return Result.success("修改分类成功");
        } catch (Exception e) {
            return Result.error(ErrorCode.UNAUTHORIZED, e.getMessage());
        }
    }

    //修改-分类发布状态
    @PutMapping("/state")
    @Operation(summary = "修改-分类发布状态", description = "修改-分类发布状态")
    public Result<String> updateCategoryState(@RequestBody CategoryDTO categoryDTO) {
        try {
            categoryService.updateCategoryState(categoryDTO);
            return Result.success("申请成功，请等待管理员审核！");
        } catch (Exception e) {
            return Result.error(ErrorCode.UNAUTHORIZED, e.getMessage());
        }
    }

    //删除分类
    @DeleteMapping()
    @Operation(summary = "删除分类", description = "删除分类")
    public Result<String> deleteCategory(@RequestParam Integer id) {
        try {
            categoryService.deleteById(id);
            return Result.success("删除分类成功");
        } catch (Exception e) {
            return Result.error(ErrorCode.UNAUTHORIZED, e.getMessage());
        }
    }
}
